package com.czollmann.android.activities;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;

import com.czollmann.android.Constants;
import com.czollmann.android.R;

public class SettingsDisplayActivity extends SettingsBaseActivity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.settings_display);
    final SharedPreferences settings = getSharedPreferences(Constants.SETTINGS_NAME, 0);
    /**
     * check listener
     */
    OnCheckedChangeListener onCheckedChangeListener = new OnCheckedChangeListener() {
      @Override
      public void onCheckedChanged(CompoundButton button, boolean checked) {
        SharedPreferences.Editor editor = settings.edit();
        if (button.getId() == R.id.setting_showcalls) {
          editor.putBoolean(Constants.SETTINGS_SHOWCALLS, checked);
        } else if (button.getId() == R.id.setting_groupcalls) {
          editor.putBoolean(Constants.SETTINGS_GROUPCALLS, checked);
        } else if (button.getId() == R.id.setting_showmessages) {
          editor.putBoolean(Constants.SETTINGS_SHOWMESSAGES, checked);
        } else if (button.getId() == R.id.setting_showpictures) {
          editor.putBoolean(Constants.SETTINGS_SHOWPICTURES, checked);
        } else if (button.getId() == R.id.setting_usefacebook) {
          editor.putBoolean(Constants.SETTINGS_USEFACEBOOK, checked);
        }
        editor.commit();
        hasChanged = true;
      }
    };
    /**
     * Checkboxes
     */
    final CheckBox callsGroupCheckBox = (CheckBox) findViewById(R.id.setting_groupcalls);
    if (settings.getBoolean(Constants.SETTINGS_GROUPCALLS, true)) {
      callsGroupCheckBox.setChecked(true);
    } else {
      callsGroupCheckBox.setChecked(false);
    }
    callsGroupCheckBox.setOnCheckedChangeListener(onCheckedChangeListener);
    final CheckBox callsCheckBox = (CheckBox) findViewById(R.id.setting_showcalls);
    if (settings.getBoolean(Constants.SETTINGS_SHOWCALLS, true)) {
      callsCheckBox.setChecked(true);
      callsGroupCheckBox.setEnabled(true);
    } else {
      callsCheckBox.setChecked(false);
      callsGroupCheckBox.setEnabled(false);
    }
    callsCheckBox.setOnCheckedChangeListener(onCheckedChangeListener);
    callsCheckBox.setOnClickListener(new OnClickListener() {
      public void onClick(View v) {
        if (callsCheckBox.isChecked()) {
          callsGroupCheckBox.setEnabled(true);
        } else {
          callsGroupCheckBox.setEnabled(false);
        }
      }
    });
    final CheckBox messagesCheckBox = (CheckBox) findViewById(R.id.setting_showmessages);
    if (settings.getBoolean(Constants.SETTINGS_SHOWMESSAGES, true)) {
      messagesCheckBox.setChecked(true);
    } else {
      messagesCheckBox.setChecked(false);
    }
    messagesCheckBox.setOnCheckedChangeListener(onCheckedChangeListener);
    final CheckBox picturesCheckBox = (CheckBox) findViewById(R.id.setting_showpictures);
    if (settings.getBoolean(Constants.SETTINGS_SHOWPICTURES, true)) {
      picturesCheckBox.setChecked(true);
    } else {
      picturesCheckBox.setChecked(false);
    }
    picturesCheckBox.setOnCheckedChangeListener(onCheckedChangeListener);
    final CheckBox facebookCheckBox = (CheckBox) findViewById(R.id.setting_usefacebook);
    if (settings.getBoolean(Constants.SETTINGS_USEFACEBOOK, true)) {
      facebookCheckBox.setChecked(true);
    } else {
      facebookCheckBox.setChecked(false);
    }
    facebookCheckBox.setOnCheckedChangeListener(onCheckedChangeListener);
  }
}
